package org.jetnuke.modules.blog;

import org.apache.log4j.Logger;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.jforum.util.preferences.ConfigKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.util.JetUtil;

public class DeleteResource implements IAction {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(DeleteResource.class);

	String file;

	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - start"); //$NON-NLS-1$
		}

		if (ui == null || ui.getID() < 2)
			return null;

		String dir = SystemGlobals.getValue(ConfigKeys.ATTACHMENTS_STORE_DIR)
				+ "/blog/" + ui.getID() + "/";
		File f = new File(dir, file);
		if (f.exists())
			f.delete();

		JetUtil.redirect(vc, response, request.getContextPath()
				+ "/do/blog/resources");

		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - end"); //$NON-NLS-1$
		}
		return null;
	}

	public void setFile(String file) {
		this.file = file;
	}

}
